var common = require('../../../utils/common.js');
Page({
  data: {
    imgUrls:[],
    indicatorDots: true,
    autoplay: true,
    activities: [],
    specials: [],
    adActivite: {},
    articles: [],
    adSpecial: {},
    interval: 7000,
    duration: 500
  },
  onLoad: function (options) {
    this.registerEvent();
  },
  registerEvent: function () {
    this.reqActivities();
    this.reqArticles();
    this.reqAdvertisements();
  },
  changeIndicatorDots: function (e) {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },
  changeAutoplay: function (e) {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },
  intervalChange: function (e) {
    this.setData({
      interval: e.detail.value
    })
  },
  durationChange: function (e) {
    this.setData({
      duration: e.detail.value
    })
  },
  reqActivities: function () {
    // let that = this;
    // common.asynPost(common.API.recommend.activities, {
    //   position: 2,
    //   take: 100,
    //   skip: 0,
    // }, function (data) {
    //   if (data['activities']) {
    //     var tempActivite = [];
    //     var tempSpecial = [];
    //     var _a, _s;
    //     for (var i = 0, len = data['activities'].length; i < len; i++) {
    //       var item = data['activities'][i];
    //       if (item.plateType == 1) {
    //         if (!_a) {
    //           _a = item;
    //           continue;
    //         }
    //         if (tempActivite.length > 0 && tempActivite[tempActivite.length - 1] && tempActivite[tempActivite.length - 1].length === 1) {
    //           tempActivite[tempActivite.length - 1].push(item);
    //         } else {
    //           tempActivite[tempActivite.length] = new Array(item);
    //         }
    //       } else {
    //         if (!_s) {
    //           _s = item;
    //           continue;
    //         }
    //         if (tempSpecial.length > 0 && tempSpecial[tempSpecial.length - 1] && tempSpecial[tempSpecial.length - 1].length === 1) {
    //           tempSpecial[tempSpecial.length - 1].push(item);
    //         } else {
    //           tempSpecial[tempSpecial.length] = new Array(item);
    //         }
    //       }
    //     }
    //     that.setData({
    //       activities: tempActivite,
    //       specials: tempSpecial,
    //       adActivite: _a,
    //       adSpecial: _s
    //     });
    //   }
    // });
  },
  onReachBottom: function () { //触底开始下一页
    var that=this;
    var pagenum = that.data.pagenum + 1; //获取当前页数并+1
    that.setData({
      pagenum: pagenum, //更新当前页数
    })
    console.log('加载成功');//重新调用请求获取下一页数据
  },
  reqArticles: function () {
    // let that = this;
    // common.asynPost(common.API.recommend.articles, {
    //   position: 2,
    //   take: 100,
    //   skip: 0
    // }, function (data) {
    //   if (data['articles']) {
    //     that.setData({
    //       articles: data['articles']
    //     });
    //   }
    // });
  },
  reqAdvertisements:function(){
    // let that = this;
    // common.asynPost(common.API.activity.advertisements,{
    //   plateType:1,
    //   position:1
    // },function(data){
    //   if(data['advertisements']){
    //     that.setData({
    //       imgUrls:data['advertisements']
    //     });
    //   }
    // },function(err){
    //   console.log(err);
    // });
  },
  timeAgo: function (date) {
    return date;
  },
  activityMore: function (e) {
    wx.navigateTo({
      url: '../recommend/recommend?type=' + 1
    });
  },
  specialMore: function (e) {
    wx.navigateTo({
      url: '../recommend/recommend?type=' + 2
    });
  },
  articleMore: function (e) {
    wx.navigateTo({
      url: '../../article/articles/articles'
    });
  },
  handleActivity: function (e) {
    let that = this, id = e.currentTarget.dataset.id, type = e.currentTarget.dataset.type;
    if (type == 1) {
      wx.navigateTo({
        url:'../../activity/detail/detail?id=' + id
      });
    } else {
      wx.navigateTo({
        url:'../../activity/special/special?id=' + id
      });
    }
  },
  handleArticle: function (e) {
    let that = this, id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url:'../../article/detail/detail?id=' + id
    });
  },
  handleAdvertisements:function(e){
    let that = this,url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url
    });
  }
})